﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Presentation.Module.Security.Interfaces.Views;
using Presentation.Module.Security.Interfaces.ViewModels;
using Presentation.Shell.Interfaces;
using Microsoft.Practices.Prism.Commands;

namespace Presentation.Module.Security.Views
{
    /// <summary>
    /// Interaction logic for AddUserView.xaml
    /// </summary>
    public partial class AddUserView : UserControl, IAddUserView
    {
        private IAddUserViewModel _addUserViewModel;

        public AddUserView(IAddUserViewModel addUserViewModel)
        {
            InitializeComponent();
            this.DataContext = addUserViewModel;
            this._addUserViewModel = addUserViewModel;
            this._addUserViewModel.View = this;
            this._addUserViewModel.Exit += this.Exit;
            this._addUserViewModel.AddingAUser += this.OnAddUser;
        }

        public event ViewExit ViewExit;

        private void Cancel(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.ViewExit != null)
            {
                this.ViewExit.Invoke();
            }
        }

        private void Exit()
        {
            if (this.ViewExit != null)
            {
                this.ViewExit.Invoke();
            }
        }

        private void OnAddUser()
        {
            _addUserViewModel.FirstPassword = txtPassword.Password;
            _addUserViewModel.RepeatPassword = txtRepPassword.Password;
        }

        private void UpdateControls(object sender, RoutedEventArgs e)
        {
            BindingExpression bindExp = ((ItemsControl)e.OriginalSource).GetBindingExpression(ItemsControl.ItemsSourceProperty);

            bindExp.UpdateSource();
            bindExp.UpdateTarget();
            this.InvalidateVisual();
        }
    }
}
